<template>
  <div class="bg-white">
    <Card>
      <SearchForm v-bind="props" @form="getFormHandler"></SearchForm>
    </Card>
    <Card>
      <StatisticTable
        :child-params="childParams"
        :columns="tenantColumns"
        :default-params="defaultParams"
        :flag="flag"
        :get-sub-table-data="getTenantSubList"
        :get-table-data="getTenantList"
        :inner-columns="apiColumns"
        :primary-params="primaryParams"
        class="main-table"
        sticky
        :bordered="true"
        :perName="'tenantDetail'"
      ></StatisticTable>
    </Card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { Card } from 'ant-design-vue';
import StatisticTable from '/@/views/measurement-center/function-statistic/component/statisticTable.vue';
import SearchForm from '/@/views/measurement-center/function-statistic/component/searchForm.vue';
import { apiColumns, tenantColumns } from '/@/views/measurement-center/function-statistic/component/tenant';
import { getTenantList, getTenantSubList } from '/@/api/measurement-center/function-statistic/functionStatistic';

const primaryParams = ref({});
const childParams = ref({});
const defaultParams = ref({
  startDate: new Date(new Date().getTime() + 8 * 60 * 60 * 1000 - 7 * 24 * 60 * 60 * 1000)
    .toJSON()
    .split('T')
    .join(' ')
    .substr(0, 19)
    .split(' ')[0],
  endDate: new Date(new Date().getTime() + 8 * 60 * 60 * 1000)
    .toJSON()
    .split('T')
    .join(' ')
    .substr(0, 19)
    .split(' ')[0],
});
const props = ref({
  type: '2',
});
const flag = ref(1);

function getFormHandler(data) {
  if (!data.date || data.date.length !== 2) {
    data.date = [];
    data.date[0] = defaultParams.value.startDate;
    data.date[1] = defaultParams.value.endDate;
  } else {
    if (data.date[0] instanceof Object) {
      data.date[0] = data.date[0].format('YYYY-MM-DD');
      data.date[1] = data.date[1].format('YYYY-MM-DD');
    }
  }

  primaryParams.value = {
    tenantList: data.tenantList,
    startDate: data.date[0],
    endDate: data.date[1],
  };
  childParams.value = {
    startDate: data.date[0],
    endDate: data.date[1],
  };
  flag.value++;
}
</script>
